ConfPurchase = new Object();

var formpanel;
// custom Vtype for vtype:'Rate'
Ext.apply(Ext.form.field.VTypes, {
	Rate : function(v) {
		return /^100(\.0{0,2})? *%?$|^\d{1,2}(\.\d{1,2})? *%?$/.test(v);
	},
	RateText : 'Invalid Rate',
	RateMask : /[\d\.%]/i
});

var confrimcalculationtype = "DEAL";

var confsearchDeal = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'status', 'name', 'insType', 'listedInsCode', 'insCode', 'dealValue', 'maturityValue', 'maturityDate', 'valueDate', 'placementFee', 'secMarket',
			'secSellerName', 'secSellerBankNo', 'couponDates', 'primaryDealer', 'issuer', 'couponRate', 'couponFreq', 'rateinterest', 'rateResetPeriod', 'resetDate', 'benchMark',
			'floorValue', 'ceilingValue', 'withholdingTax', 'withholdingTaxRate', 'dayBasis', 'denominalMinVal', 'user', 'dealCode', 'fund', 'params', 'settlementCode',
			'delevarymethode', 'dealerCode' ]
});

var confinstrumentStore = new Ext.data.Store({
	fields : [ 'id', 'couponRate', 'label', 'issuer', 'minDenominal', 'listedInsCode', 'valueDate', 'maturityDate', 'status', 'issuerName' ],
	storeId : 'recPurchInsStore'
});

ConfPurchase.getBasicFieldSet = function() {

	var funds = Ext.create('Ext.data.Store', {
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'fundName',
			type : 'string',
		}, {
			name : 'fundCode',
			type : 'string',
		} ],
		storeId : 'dealFundsStore',
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Basic',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'label',
			id : 'confirmPurchParamLbl',
			hidden : true,
			text : '',
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ Ext.create('Ext.form.ComboBox', {
				name : 'fund',
				fieldLabel : 'Fund',
				store : funds,
				queryMode : 'local',
				flex : 1,
				id : 'confirmPurchFund',
				padding : '0 5 5 0',
				valueField : 'id',
				displayField : 'fundName',
				disabled : true,
				autoScroll : true,
				allowBlank : false,
				blankText : 'Please select the Fund',
				msgTarget : 'under',
				editable : false,
			}), {
				xtype : 'textfield',
				name : 'name',
				id : 'confirmPurchInsName',
				fieldLabel : 'Name',
				disabled : true,
				flex : 1,
				allowBlank : false,
				padding : '0 5 5 0',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'datefield',
				fieldLabel : 'Value Date',
				name : 'valueDate',
				id : 'confirmPurchValueDt',
				allowBlank : false,
				disabled : true,
				flex : 1,
				padding : '0 5 5 0',
				blankText : 'Please select a Value Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				submitFormat : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				listeners : {
					'change' : {
						fn : function(field, newVal, oldVal) {
							var dfMaturityDate = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];
							if (dfMaturityDate.getValue() != null) {
								var lblMaturityDate = Ext.ComponentQuery.query('#confirmPurchDuration')[0];
								if (this.getValue() > dfMaturityDate.getValue()) {
									Ext.MessageBox.alert('Error', 'Value Date cannot be greater than Maturity Date.');
									this.setValue(null);
									lblMaturityDate.setText('');
								} else {
									var durationDays = (dfMaturityDate.getValue() - this.getValue()) / 86400000;
									lblMaturityDate.setText('Duration: '.concat(durationDays.toString().concat(' days.')));
								}
							}
						}
					}
				}

			}, {
				xtype : 'datefield',
				fieldLabel : 'Matured Date',
				id : 'confirmPurchMaturityDt',
				name : 'maturityDate',
				allowBlank : false,
				disabled : true,
				flex : 1,
				padding : '0 5 5 0',
				blankText : 'Please select a Matured Date',
				msgTarget : 'under',
				format : 'd/m/Y',
				submitFormat : 'd/m/Y',
				emptyText : 'DD/MM/YYYY',
				listeners : {
					'change' : {
						fn : function(field, newVal, oldVal) {
							var dfValueDate = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
							if (dfValueDate.getValue() != null) {
								var lblValueDate = Ext.ComponentQuery.query('#confirmPurchDuration')[0];
								if (dfValueDate.getValue() > this.getValue()) {
									Ext.MessageBox.alert('Error', 'Maturity Date cannot be smaller than Value Date.');
									this.setValue(null);
									lblValueDate.setText('');
								} else {
									var durationDays = (this.getValue() - dfValueDate.getValue()) / 86400000;
									lblValueDate.setText('Duration: '.concat(durationDays.toString().concat(' days.')));
								}

							}
						}
					}
				}
			} ]
		}, {
			xtype : 'label',
			id : 'confirmPurchDuration'
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'combo',
				fieldLabel : 'Delevary Method',
				store : CommenStore.delevaryMethodStore,
				valueField : 'id',
				flex : 1,
				id : 'confPurchaseDelevaryMethod',
				labelWidth : 120,
				name : 'delevarymethode',
				padding : '0 5 0 0',
				disabled : true,
				displayField : 'code',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{description}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {description}', '</tpl>'),
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
							combo.setValue(null);
						}
					}
				}
			}, {
				xtype : 'combo',
				fieldLabel : 'Dealer',
				store : CommenStore.dealersStore,
				valueField : 'id',
				flex : 1,
				id : 'confPurchaseDealer',
				padding : '0 5 0 0',
				name : 'dealerCode',
				disabled : true,
				displayField : 'code',
				tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{name}</b><br/></div>', '</tpl>'),
				displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {name}', '</tpl>'),
				listeners : {
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
							combo.setValue(null);
						}
					}
				}
			} ]
		}, {
			xtype : 'combo',
			labelWidth : 120,
			fieldLabel : 'Settlement Method',
			store : CommenStore.settlmentStore,
			valueField : 'id',
			id : 'confPurchaseSettlementMethod',
			anchor : '50%',
			name : 'settlementCode',
			padding : '0 10 0 0',
			displayField : 'code',
			disabled : true,
			tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{description}</b><br/></div>', '</tpl>'),
			displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code} - {description}', '</tpl>'),
			listeners : {
				change : function(combo, record, index) {
					if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0) {
						combo.setValue(null);
					}
				}
			}
		} ]
	});
};

ConfPurchase.getInstrumentFieldSet = function() {

	Ext.define('InstrumentTypePurch', {
		extend : 'Ext.data.Model',
		fields : [ {
			name : 'id',
			type : 'int'
		}, {
			name : 'instrumentName',
			type : 'string'
		}, {
			name : 'istrumentType',
			type : 'int'
		}, {
			name : 'scripts',
			type : 'int'
		}, {
			name : 'denominals',
			type : 'int'
		}, {
			name : 'listed',
			type : 'int'
		}, {
			name : 'withholdingTax',
			type : 'string'
		}, {
			name : 'interestRate',
			type : 'string'
		}, {
			name : 'interestType',
			type : 'string'
		}, {
			name : 'dayBasis',
			type : 'string'
		}, {
			name : 'couponAvailability',
			type : 'int'
		}, {
			name : 'cuponFrequency',
			type : 'int'
		}, {
			name : 'shortMaturable',
			type : 'int'
		} ]
	});

	var instrumentTypeStore = new Ext.data.Store({
		model : 'InstrumentTypePurch',
		storeId : 'confirmPurchInsTypeStore',
	});

	Ext.Ajax.request({
		url : 'getInstrumentTypes.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			instrumentTypeStore.loadRawData(obj.instrumentTypeList);
		}
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Instrument',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'combo',
				name : 'insType',
				id : 'confirmPurchInstrumentType',
				fieldLabel : 'Instrument Type',
				disabled : true,
				queryMode : 'local',
				store : instrumentTypeStore,
				valueField : 'id',
				displayField : 'instrumentName',
				flex : 1,
				padding : '0 5 0 0',
				autoScroll : true,
				allowBlank : false,
				blankText : 'Select the Instrument Type',
				msgTarget : 'under',
				editable : false,
				listeners : {
					select : {
						fn : function(combo, value) {
							var record = instrumentTypeStore.getAt(instrumentTypeStore.find('id', combo.getValue()));
							Ext.getCmp('confirmPurchInstrumentCodeCombo').clearValue();
							Ext.getCmp('confirmPurchFund').clearValue();
							console.log("1");
							ConfPurchase.fieldDisableByInstrumentType(record);
							Ext.Ajax.request({
								url : 'getFundsForDealInsType.action',
								params : {
									querry : 'PURCH',
									querryTwo : record.get('id')
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);
								}
							});
						}
					},
					change : function(list, record) {
						if (record) {
							var record1 = instrumentTypeStore.getAt(instrumentTypeStore.find('id', list.getValue()));
							ConfPurchase.fieldDisableByInstrumentType(record1);
							Ext.Ajax.request({
								url : 'getFundsForDealInsType.action',
								params : {
									querry : 'PURCH',
									querryTwo : list.getValue()
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);
								}
							});
						}
					}
				}
			}, {
				xtype : 'displayfield',
				flex : 1,
				value : '',
				style : 'font-weight:600;',
				id : 'confirmPurchListedLabel',
			} ]
		}, {
			xtype : 'combo',
			name : 'listedInsCode',
			id : 'confirmPurchInstrumentCodeCombo',
			fieldLabel : 'Instrument Code',
			store : confinstrumentStore,
			queryMode : 'local',
			valueField : 'code',
			displayField : 'code',
			anchor : '50%',
			padding : '0 5 0 0',
			style : 'margin-top:10px;',
			disabled : true,
			autoScroll : true,
			allowBlank : false,
			blankText : 'Select the Instrument Code',
			msgTarget : 'under',
			editable : false,
			hidden : true,
			tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item">{code} - <b>{issuer}</b><br/></div>', '</tpl>'),
			displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{code}', '</tpl>'),
			listConfig : {
				listeners : {
					itemclick : function(list, record) {
						ConfPurchase.setInstrumentValue(record);
					}
				}
			}
		}, {
			xtype : 'textfield',
			name : 'insCode',
			fieldLabel : 'Instrument Code',
			id : 'confirmPurchInstrumentCodeText',
			allowBlank : false,
			disabled : true,
			anchor : '50%',
			padding : '0 5 0 0',
			style : 'margin-top:10px;',
			blankText : 'Instrument Code is required',
			msgTarget : 'under',
			listeners : {
				'blur' : function(field) {
					find = instrumentStore.find('code', field.getValue());
					if (find != -1) {
						Ext.Msg.alert(field.getValue() + " is already used!");
						this.reset();
					}
				}
			}
		}, {
			xtype : 'label',
			flex : 1,
			id : 'confirmPurchDealID',
			style : 'font-weight:600;margin-top:15px;',
			text : 'Deal ID: ',
			hidden : true,
		}, {
			xtype : 'displayfield',
			flex : 1,
			id : 'confirmPurchStatusLbl',
			style : 'font-weight:600;margin-top:15px;',
			hidden : true,
		} ]
	});
};

ConfPurchase.getDealFieldSet = function() {
	return Ext.create('Ext.form.FieldSet', {
		title : 'Deal',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				id : 'confirmPurchDealVal',
				minValue : 0,
				name : 'dealValue',
				fieldLabel : 'Deal Value',
				allowBlank : false,
				disabled : true,
				blankText : 'Please enter the Deal Amount',
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			}, {
				xtype : 'textfield',
				minValue : 0,
				name : 'placementFee',
				id : 'confirmPurchPlacementFee',
				fieldLabel : 'Placement Fee',
				allowBlank : false,
				disabled : true,
				blankText : 'Please enter the Placement Fee',
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'checkboxfield',
				boxLabel : 'Secondary Market',
				id : 'confirmPurchSecMarket',
				name : 'secMarket',
				inputValue : 1,
				flex : 1,
				padding : '0 5 5 0',
				listeners : {
					change : {
						fn : function(object, Value) {
							if (Value) {
								var ssname = Ext.ComponentQuery.query('#secSellerName')[0];
								ssname.enable();
								var ssbank = Ext.ComponentQuery.query('#secSellerBank')[0];
								ssbank.enable();
							} else {
								var ssname = Ext.ComponentQuery.query('#secSellerName')[0];
								ssname.disable();
								var ssbank = Ext.ComponentQuery.query('#secSellerBank')[0];
								ssbank.disable();
							}
						}
					}
				}
			}, {
				xtype : 'textfield',
				minValue : 0,
				id : 'confirmPurchMaturityValue',
				fieldLabel : 'Maturity Value',
				name : 'maturityValue',
				disabled : true,
				msgTarget : 'under',
				flex : 1,
				padding : '0 5 5 0',
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value.replace(/,/g, ''), '0,000.0000'));
					},
					render : function(p) {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			}

			]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			style : 'margin-top:10px;',
			items : [ {
				xtype : 'textfield',
				name : 'secSellerName',
				id : 'secSellerName',
				fieldLabel : "Seller's Name",
				disabled : true,
				flex : 1,
				padding : '0 5 5 0',
			}, {
				xtype : 'textfield',
				name : 'secSellerBankNo',
				id : 'secSellerBank',
				fieldLabel : "Bank No",
				disabled : true,
				flex : 1,
				padding : '0 5 5 0',
			} ]
		} ]
	});
};

ConfPurchase.getInstrumentDetailFieldSet = function() {

	var cpnDtStore = Ext.create('Ext.data.Store', {
		storeId : 'couponDatesStore',
		fields : [ {
			name : 'couponDate',
			type : 'date',
			dateFormat : 'd/m/Y'
		} ]
	});

	var primaryDealerStore = Ext.create('Ext.data.Store', {
		storeId : 'primaryDealersStore',
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'name',
			type : 'string',
		}, {
			name : 'code',
			type : 'string',
		}, {
			name : 'creditRating',
			type : 'string',
		} ]
	});

	var issuersStore = Ext.create('Ext.data.Store', {
		storeId : 'issuersStore',
		fields : [ {
			name : 'id',
			type : 'int',
		}, {
			name : 'name',
			type : 'string',
		}, {
			name : 'code',
			type : 'string',
		}, {
			name : 'creditRating',
			type : 'string',
		} ]
	});

	Ext.Ajax.request({
		url : 'getPrimaryDealers.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			primaryDealerStore.loadRawData(obj.issuers);
		}
	});

	Ext.Ajax.request({
		url : 'getIssuers.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			issuersStore.loadRawData(obj.issuerList);
		}
	});

	var valDate = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
	var matDate = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];

	return Ext.create('Ext.form.FieldSet', {
		title : 'Instrument Details',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'form',
			bodyBorder : false,
			frame : true,
			border : false,
			id : 'confinstrumentdetails',
			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'radiogroup',
					fieldLabel : 'Instrument Type',
					labelWidth : 110,
					columns : 1,
					flex : 1,
					padding : '0 5 0 0',
					vertical : true,
					disabled : true,
					items : [ {
						boxLabel : 'Government',
						name : 'istrumentType',
						inputValue : '1',
						checked : true,
						id : 'cbIssuerGovernment',
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
									Ext.getCmp('recordDealPrimDealer').setDisabled(false);
									Ext.getCmp('recordDealPrimDealer').allowBlank = false;
									Ext.getCmp('recordDealIssuer').setDisabled(true);
									Ext.getCmp('recordDealIssuer').allowBlank = true;
								}
							}
						}
					}, {
						boxLabel : 'Other',
						name : 'istrumentType',
						inputValue : '0',
						id : 'cbIssuerOther',
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
									Ext.getCmp('recordDealPrimDealer').setDisabled(true);
									Ext.getCmp('recordDealPrimDealer').allowBlank = true;
									Ext.getCmp('recordDealIssuer').setDisabled(false);
									Ext.getCmp('recordDealIssuer').allowBlank = false;
								}
							}
						}
					} ]
				}, {
					xtype : 'fieldcontainer',
					flex : 1,
					layout : {
						type : 'vbox',
					},
					items : [ {
						xtype : 'combo',
						fieldLabel : 'Primary Dealer',
						store : primaryDealerStore,
						valueField : 'id',
						name : 'primaryDealer',
						queryMode : 'local',
						id : 'recordDealPrimDealer',
						autoScroll : true,
						anchor : '50%',
						editable : false,
						tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{code}</b>-{name}<br/></div>', '</tpl>'),
						displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>')
					}, {
						xtype : 'combo',
						fieldLabel : 'Issuer',
						store : issuersStore,
						valueField : 'id',
						name : 'issuer',
						anchor : '50%',
						queryMode : 'local',
						id : 'recordDealIssuer',
						autoScroll : true,
						editable : false,
						disabled : true,
						tpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '<div class="x-boundlist-item"><b>{code}</b>-{name}<br/></div>', '</tpl>'),
						displayTpl : Ext.create('Ext.XTemplate', '<tpl for=".">', '{name}', '</tpl>')
					} ]
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'radiogroup',
					fieldLabel : 'Coupon Availability',
					labelWidth : 110,
					columns : 3,
					columnWidth : 5,
					flex : 1,
					// id : 'confirmPurchCouponField',
					padding : '0 5 0 0',
					vertical : true,
					disabled : true,
					items : [ {
						boxLabel : 'No',
						name : 'couponAvailability',
						inputValue : '0',
						id : 'confirmPurchCpnNo',
						checked : true,
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
									var whtrate = Ext.getCmp('confirmPurchCouponRate');
									whtrate.setValue("");
									whtrate.disable();
									whtrate.allowBlank = true;

									Ext.getCmp('confirmPurchCouponFreq').hide();
									Ext.getCmp('confirmDealCoupnData').hide();
									Ext.getCmp('confirmDealCoupnData').disable();
								}
							}
						}
					}, {
						boxLabel : 'Fixed',
						name : 'couponAvailability',
						inputValue : '1',
						id : 'confirmPurchCpnFixcb',
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
									Ext.getCmp('confirmPurchCouponRate').setDisabled(false);
									Ext.getCmp('confirmPurchCouponRate').allowBlank = false;
									Ext.getCmp('confirmPurchCouponFreq').show();
									Ext.getCmp('confirmDealCoupnData').hide();
									Ext.getCmp('confirmDealCoupnData').disable();
								}
							}
						}
					}, {
						boxLabel : 'Vary',
						name : 'couponAvailability',
						inputValue : '2',
						id : 'confirmPurchCpnVarycb',
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
									Ext.getCmp('confirmPurchCouponRate').setDisabled(false);
									Ext.getCmp('confirmPurchCouponRate').allowBlank = false;
									Ext.getCmp('confirmPurchCouponFreq').hide();
									Ext.getCmp('confirmDealCoupnData').show();
									Ext.getCmp('confirmDealCoupnData').enable();
								}
							}
						}
					} ]
				}, {
					xtype : 'textfield',
					vtype : 'Rate',
					validateOnBlur : true,
					maskRe : /[\d\%.]/,
					flex : 1,
					padding : '0 5 0 0',
					name : 'couponRate',
					id : 'confirmPurchCouponRate',
					fieldLabel : 'Coupon Rate',
					blankText : 'Coupon Rate is required',
					msgTarget : 'under',
					disabled : true,
					listeners : {
						blur : function() {
							var value = Ext.util.Format.number(this.value.replace(/%/g, ''), '000.00');
							this.setRawValue(value + "%");
						}
					}
				} ]
			}, {
				xtype : 'numberfield',
				minValue : 0,
				labelWidth : 115,
				maxValue : 100,
				decimalPrecision : 0,
				name : 'couponFreq',
				anchor : '50%',
				id : 'confirmPurchCouponFreq',
				fieldLabel : 'Coupon Frequency',
				disabled : true,
				hidden : true,
				allowBlank : false
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				hidden : true,
				id : 'confirmDealCoupnData',
				items : [ {
					xtype : 'datefield',
					fieldLabel : 'Coupon Dates',
					id : 'confirmPurchCpnDts',
					minValue : new Date(),
					flex : 1,
					padding : '0 5 0 0',
					labelWidth : 115,
					blankText : 'Please select a Start Date',
					msgTarget : 'under',
					format : 'd/m/Y',
					emptyText : 'DD/MM/YYYY',
					listeners : {
						change : {
							fn : function(object, newVal, oldVal) {
								var recordIndex = cpnDtStore.findBy(function(record, id) {
									if (newVal != null && (record.get('couponDate').getTime() == newVal.getTime())) {
										return true;
									}
									return false;
								});
								if (recordIndex != -1) {
									Ext.MessageBox.alert('Error!', 'Duplicate Entry!!!');
								} else if (newVal == null) {
									Ext.MessageBox.alert('Error', 'Please Select a Start Date');
									console.log("Null value for date field");
								} else if (valDate.getValue() == null || matDate.getValue() == null) {
									Ext.MessageBox.alert('Error!', 'Deal Period not specified!');
								} else if (newVal < valDate.getValue() || newVal > matDate.getValue()) {
									Ext.MessageBox.alert('Error!', 'Coupon Date should be within the Deal Period!');
								} else {
									cpnDtStore.add({
										couponDate : Ext.util.Format.date(newVal, 'd/m/Y')
									});
								}
							}
						}
					}
				}, Ext.create('Ext.grid.Panel', {
					width : 100,
					height : 130,
					flex : 1,
					store : cpnDtStore,
					id : 'recordDealDtStore',
					autoScroll : true,
					columns : [ {
						xtype : 'datecolumn',
						header : 'Coupon Dates',
						dataIndex : 'couponDate',
						format : 'd/m/Y',
						flex : 1
					} ],
					bbar : [ {
						xtype : 'button',
						text : 'Remove',
						handler : function() {
							var pCpnDtStore = Ext.getCmp('recordDealDtStore');
							var sm = pCpnDtStore.getSelectionModel();
							var sel = sm.getSelection();
							var i = 0;
							var currentDate = new Date();
							for (i = 0; i < sel.length; i++) {
								if (sel[i].get("couponDate") > currentDate) {
									pCpnDtStore.store.remove(sel[i]);
								} else {
									Ext.Msg.alert('Error', 'Cannot Remove Previous Dates!');
								}
							}
						}
					} ]
				}) ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'radiogroup',
					fieldLabel : 'Interest Type',
					labelWidth : 110,
					columns : 2,
					flex : 1,
					columnWidth : 5,
					padding : '0 5 0 0',
					vertical : true,
					disabled : true,
					items : [ {
						boxLabel : 'Fixed',
						id : 'confirmPurchIntTypeFix',
						name : 'interestRate',
						inputValue : 'Fixed',
						checked : true,
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
									Ext.getCmp('confirmPurchFloatField1').hide();
									Ext.getCmp('confirmPurchFloatField2').hide();
									Ext.getCmp('confirmPurchFloatField1').disable();
									Ext.getCmp('confirmPurchFloatField2').disable();
									Ext.getCmp('confirmPurchResetPeriod').allowBlank = true;
									Ext.getCmp('confirmPurchFloatResetDt').allowBlank = true;
									Ext.getCmp('confirmPurchFloor').allowBlank = true;
									Ext.getCmp('confirmPurchCeiling').allowBlank = true;
									Ext.getCmp('confirmPurchResetPeriod').disable();
									Ext.getCmp('confirmPurchFloatResetDt').disable();
									Ext.getCmp('confirmPurchFloor').disable();
									Ext.getCmp('confirmPurchCeiling').disable();
								} else {
									Ext.getCmp('confirmPurchFloatField1').show();
									Ext.getCmp('confirmPurchFloatField2').show();
									Ext.getCmp('confirmPurchFloatField1').enable();
									Ext.getCmp('confirmPurchFloatField2').enable();
									Ext.getCmp('confirmPurchResetPeriod').allowBlank = false;
									Ext.getCmp('confirmPurchFloatResetDt').allowBlank = false;
									Ext.getCmp('confirmPurchFloor').allowBlank = false;
									Ext.getCmp('confirmPurchCeiling').allowBlank = false;
									Ext.getCmp('confirmPurchResetPeriod').enable();
									Ext.getCmp('confirmPurchFloatResetDt').enable();
									Ext.getCmp('confirmPurchFloor').enable();
									Ext.getCmp('confirmPurchCeiling').enable();
								}
							}
						}
					}, {
						boxLabel : 'Floating',
						id : 'confirmPurchIntTypeFloat',
						name : 'interestRate',
						inputValue : 'Floating',
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
									console.log(newValue);
									Ext.getCmp('confirmPurchFloatField1').show();
									Ext.getCmp('confirmPurchFloatField2').show();
									Ext.getCmp('confirmPurchFloatField1').enable();
									Ext.getCmp('confirmPurchFloatField2').enable();
									Ext.getCmp('confirmPurchResetPeriod').allowBlank = false;
									Ext.getCmp('confirmPurchFloatResetDt').allowBlank = false;
									Ext.getCmp('confirmPurchFloor').allowBlank = false;
									Ext.getCmp('confirmPurchCeiling').allowBlank = false;
									Ext.getCmp('confirmPurchResetPeriod').enable();
									Ext.getCmp('confirmPurchFloatResetDt').enable();
									Ext.getCmp('confirmPurchFloor').enable();
									Ext.getCmp('confirmPurchCeiling').enable();
								} else {
									console.log(newValue);
									Ext.getCmp('confirmPurchFloatField1').hide();
									Ext.getCmp('confirmPurchFloatField2').hide();
									Ext.getCmp('confirmPurchFloatField1').disable();
									Ext.getCmp('confirmPurchFloatField2').disable();
									Ext.getCmp('confirmPurchResetPeriod').allowBlank = true;
									Ext.getCmp('confirmPurchFloatResetDt').allowBlank = true;
									Ext.getCmp('confirmPurchFloor').allowBlank = true;
									Ext.getCmp('confirmPurchCeiling').allowBlank = true;
									Ext.getCmp('confirmPurchResetPeriod').disable();
									Ext.getCmp('confirmPurchFloatResetDt').disable();
									Ext.getCmp('confirmPurchFloor').disable();
									Ext.getCmp('confirmPurchCeiling').disable();
								}
							}
						}
					} ]
				}, {
					xtype : 'textfield',
					vtype : 'Rate',
					validateOnBlur : true,
					maskRe : /[\d\%.]/,
					flex : 1,
					name : 'rateinterest',
					padding : '0 5 0 0',
					fieldLabel : 'Rate',
					id : 'confirmPurchInterestRate',
					allowBlank : false,
					blankText : 'Rate is required',
					msgTarget : 'under',
					listeners : {
						blur : function() {
							var value = Ext.util.Format.number(this.value.replace(/%/g, ''), '000.00');
							this.setRawValue(value + "%");
						}
					}
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				id : 'confirmPurchFloatField1',
				hidden : true,
				items : [ {
					xtype : 'numberfield',
					id : 'confirmPurchResetPeriod',
					labelWidth : 115,
					fieldLabel : 'Reset Period',
					name : 'rateResetPeriod',
					flex : 1,
					allowBlank : true,
					minValue : 1,
					decimalPrecision : 0,
					padding : '0 5 0 0'
				}, {
					xtype : 'datefield',
					fieldLabel : 'Reset Date',
					id : 'confirmPurchFloatResetDt',
					flex : 1,
					allowBlank : true,
					padding : '0 5 0 0',
					format : 'd/m/Y',
					emptyText : 'DD/MM/YYYY',
					minValue : new Date(),
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				id : 'confirmPurchFloatField2',
				hidden : true,
				items : [ {
					xtype : 'combo',
					labelWidth : 115,
					fieldLabel : 'Bench Mark',
					store : benchMarkStore,
					displayField : 'name',
					valueField : 'name',
					name : 'benchMark',
					id : 'confirmPurchBench',
					flex : 1.5,
					padding : '0 5 0 0',
					typeAhead : true,
					typeAheadDelay : 100,
					listConfig : {
						loadingText : 'Searching...',
						emptyText : 'No matching posts found.'
					}
				}, {
					xtype : 'numberfield',
					id : 'confirmPurchFloor',
					minValue : 0,
					maxValue : 100,
					decimalPrecision : 4,
					name : 'floorValue',
					labelWidth : 50,
					allowBlank : true,
					fieldLabel : 'Floor',
					flex : 1,
					padding : '0 5 0 0'
				}, {
					xtype : 'numberfield',
					id : 'confirmPurchCeiling',
					minValue : 0,
					maxValue : 100,
					decimalPrecision : 4,
					name : 'ceilingValue',
					fieldLabel : 'Ceiling',
					labelWidth : 50,
					flex : 1,
					allowBlank : true,
					padding : '0 5 0 0'
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'radiogroup',
					fieldLabel : 'Withholding Tax',
					labelWidth : 110,
					columns : 3,
					columnWidth : 5,
					flex : 1,
					padding : '0 5 0 0',
					vertical : true,
					disabled : true,
					items : [ {
						boxLabel : 'No',
						id : 'confirmPurchWhTNo',
						name : 'withholdingTax',
						inputValue : 'No',
						checked : true,
						disabled : true,
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
									var whtrate = Ext.getCmp('confirmWithholdingTaxRate');
									whtrate.setValue("");
									whtrate.disable();
									whtrate.allowBlank = true;
								} else {
									var whtrate = Ext.getCmp('confirmWithholdingTaxRate');
									whtrate.setValue("");
									whtrate.enable();
									whtrate.allowBlank = false;
								}
							}
						}
					}, {
						boxLabel : 'Upfront',
						id : 'confirmPurchWhTUp',
						name : 'withholdingTax',
						inputValue : 'Upfront',
						disabled : true,
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {
								}
							}
						}
					}, {
						boxLabel : 'Maturity',
						id : 'confirmPurchWhTMat',
						name : 'withholdingTax',
						inputValue : 'Maturity',
						disabled : true,
						listeners : {
							change : function(field, newValue, oldValue) {
								if (newValue) {

								}
							}
						}
					} ]
				}, {
					xtype : 'textfield',
					vtype : 'Rate',
					validateOnBlur : true,
					maskRe : /[\d\%.]/,
					flex : 1,
					padding : '0 5 0 0',
					fieldLabel : 'Withholding Tax Rate',
					disabled : true,
					id : 'confirmWithholdingTaxRate',
					name : 'withholdingTaxRate',
					listeners : {
						blur : function() {
							var value = Ext.util.Format.number(this.value.replace(/%/g, ''), '000.00');
							this.setRawValue(value + "%");
						}
					}
				} ]
			}, {
				xtype : 'combo',
				store : new Ext.data.Store({
					fields : [ 'dayBasis' ],
					data : [ {
						"dayBasis" : "Act / Act"
					}, {
						"dayBasis" : "Act / 360"
					}, {
						"dayBasis" : "Act / 364"
					}, {
						"dayBasis" : "Act / 365"
					}, {
						"dayBasis" : "30 / 360"
					}, {
						"dayBasis" : "30 / 365"
					} ]
				}),
				displayField : 'dayBasis',
				valueField : 'dayBasis',
				name : 'dayBasis',
				mode : 'local',
				value : 'Act / 365',
				fieldLabel : 'Day Basis',
				labelWidth : 110,
				forceSelection : true,
				disabled : true,
				allowBlank : false,
				id : 'confirmPurchDayBasis',
				anchor : '50%',
				padding : '0 10 0 0',
				listeners : {
					blur : function() {

					}
				}
			}, {
				xtype : 'radiogroup',
				columns : 2,
				anchor : '50%',
				vertical : true,
				disabled : true,
				labelWidth : 170,
				fieldLabel : 'Interest Calculation Method',
				items : [ {
					boxLabel : 'Straight',
					name : 'intCalMethod',
					checked : true,
					id : 'confirmPurchIntCalStr',
					inputValue : 'straight',
				}, {
					boxLabel : 'Discounted',
					name : 'intCalMethod',
					id : 'confirmPurchIntCalDis',
					inputValue : 'discounted',
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				items : [ {
					xtype : 'checkboxfield',
					boxLabel : 'Scripts',
					name : 'scripts',
					inputValue : 1,
					id : 'confirmPurchScripts',
					disabled : true,
					padding : '0 10 0 0',
					flex : 0.5
				}, {
					xtype : 'checkboxfield',
					boxLabel : 'Denominals',
					name : 'denominals',
					inputValue : 1,
					id : 'confirmPurchdenominals',
					disabled : true,
					padding : '0 10 0 0',
					flex : 0.5
				}, {
					xtype : 'textfield',
					padding : '0 5 0 0',
					flex : 1,
					name : 'denominalMinVal',
					id : 'confirmPurchDenominamMinVal',
					fieldLabel : 'Minimum Value',
					disabled : true,
					allowBlank : false
				} ]
			} ]
		} ]
	});
};

ConfPurchase.getCenterPanel = function() {

	var basic = ConfPurchase.getBasicFieldSet();
	var instrument = ConfPurchase.getInstrumentFieldSet();
	var deal = ConfPurchase.getDealFieldSet();
	var insDtl = ConfPurchase.getInstrumentDetailFieldSet();

	CommenStore.settlmentStore.load();
	CommenStore.dealersStore.load();
	CommenStore.delevaryMethodStore.load();

	formPanel = Ext.create('Ext.form.FormPanel', {
		title : 'New Purchase Record',
		id : 'confirmPurchMainPanel',
		bodyPadding : '20',
		autoScroll : true,
		frame : true,
		defaults : {
			anchor : '72%',
		},
		fieldDefaults : {
			labelAlign : 'left',
		},
		items : [ {
			xtype : 'fieldcontainer',
			items : [ {
				xtype : 'triggerfield',
				id : 'confirmPurchTrigField',
				triggerCls : 'x-form-search-trigger',
				anchor : '28%',
				style : 'float:right;',
				emptyText : 'Search for a Purchase',
				onTriggerClick : function() {
					var resultWindow = null;
					var searchResult = Ext.create('Ext.data.Store', {
						fields : [ 'id', 'dealCode', 'name', 'status' ]
					});
					var TrigField = Ext.ComponentQuery.query('#confirmPurchTrigField')[0];

					Ext.Ajax.request({
						url : 'getPurchases.action',
						params : {
							query : TrigField.getValue()
						},
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							searchResult.loadRawData(obj.deals);
						}
					});
					if (!resultWindow) {
						resultWindow = Ext.create('Ext.window.Window', {
							title : 'Purchase Deals',
							height : 330,
							frame : true,
							width : 500,
							constrain : true,
							resizable : false,
							layout : {
								type : 'fit'
							},
							items : [ {
								xtype : 'gridpanel',
								defaults : {
									anchor : '100%'
								},
								store : searchResult,
								columns : [ {
									text : 'Deal Code',
									dataIndex : 'dealCode',
									flex : 1
								}, {
									text : 'Name',
									dataIndex : 'name',
									flex : 2
								}, {
									text : 'Status',
									dataIndex : 'status',
									flex : 1
								} ],
								listeners : {
									itemdblclick : function(view, rec, item, index, eventObj) {
										var findId = rec.get('id');
										var formPanl = Ext.getCmp('confirmPurchMainPanel');
										formPanl.getForm().reset();
										Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
										formPanl.setLoading('Receiving data...', formPanl.getForm());
										Ext.Ajax.request({
											url : 'getPurchase.action',
											params : {
												id : findId
											},
											success : function(response) {
												var obj = Ext.decode(response.responseText);
												confsearchDeal.loadRawData(obj.dealDtos);
												var record = confsearchDeal.getAt(0);
												formPanel.loadRecord(record);

												var lblDealID = Ext.getCmp('confirmPurchDealID');
												lblDealID.show();
												lblDealID.setText('Deal ID: ' + record.get('dealCode'));

												var lblDealStatus = Ext.getCmp('confirmPurchStatusLbl');
												lblDealStatus.show();
												lblDealStatus.setValue('Status: ' + record.get('status'));

												if (record.get('params') != null) {
													Ext.getCmp('confirmPurchParamLbl').setText(record.get('params'));
												}

												if (record.get('status') === 'Initial') {
													Ext.Ajax.request({
														url : 'confFields.action',
														params : {
															screen : 'RecordPurchase',
														},
														success : function(response) {
															var obj = Ext.decode(response.responseText);
															confirmFields = obj.values;
															formPanl.getForm().getFields().each(function(field) {
																field.setDisabled(true);
															});
															var confFieldsArrray = confirmFields.split(";");
															var i;
															var confFieldLocal;
															var confFieldQuery;
															for (i = 0; i < confFieldsArrray.length - 1; i++) {
																confFieldQuery = '#' + confFieldsArrray[i];
																confFieldLocal = Ext.ComponentQuery.query(confFieldQuery)[0];
																confFieldLocal.reset();
																confFieldLocal.enable();
															}
															Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();

															Ext.getCmp('confirmPurchConfirmBtn').enable();
															Ext.getCmp('confirmPurchRejectBtn').enable();
														},
														failure : function(form, action) {
															formPanel.setLoading(false);
															Ext.Msg.alert('Error!', 'Failed to load Confirmation Fields!');
														}
													});
												} else {
													formPanl.getForm().getFields().each(function(field) {
														field.setDisabled(true);
													});
													Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
													Ext.getCmp('confirmPurchConfirmBtn').disable();
													Ext.getCmp('confirmPurchRejectBtn').disable();
												}
												formPanl.setLoading(false);
											},
											failure : function(form, action) {
												formPanl.setLoading(false);
											}
										});
										resultWindow.close();
									}
								}
							} ]
						});
					}
					if (resultWindow.isVisible()) {
						resultWindow.close();
					} else {
						resultWindow.show();
					}
				}
			} ]
		}, instrument, basic, deal, insDtl ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
				Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
				Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
				confrimcalculationtype = "DEAL";
			}
		}, {
			text : 'Reject',
			id : 'confirmPurchRejectBtn',
			itemId : 'rejectButton',
			disabled : true,
			handler : function() {
				var form = this.up('form').getForm();
				form.url = 'rejectPurchase.action';
				formPanel.setLoading('Rejecting...');
				formPanel.getForm().getFields().each(function(field) {
					field.setDisabled(true);
				});
				var dealCode = Ext.ComponentQuery.query('#confirmPurchDealID')[0].text.substr(9);
				console.log('dealCode: ' + dealCode);

				form.submit({
					params : {
						'dealCode' : dealCode,
						'user' : 'updateUser',
					},
					success : function(form, action) {
						form.reset();
						Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
						Ext.Msg.alert('Success', action.result.name + ' is Rejected.<br> Deal Code: <b>' + action.result.dealCode + '</b>');
						Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
						Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
						Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
						formPanel.getForm().getFields().each(function(field) {
							field.setDisabled(true);
						});
						Ext.getCmp('confirmPurchConfirmBtn').disable();
						Ext.getCmp('confirmPurchRejectBtn').disable();
						Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						formPanel.setLoading(false);
					},
					failure : function(form, action) {
						Ext.Msg.alert('Failed', 'There is some error returned from the server.');
						Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
						Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
						Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
						formPanel.getForm().getFields().each(function(field) {
							field.setDisabled(true);
						});
						Ext.getCmp('confirmPurchConfirmBtn').disable();
						Ext.getCmp('confirmPurchRejectBtn').disable();
						Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						formPanel.setLoading(false);
						formPanel.setLoading(false);
					}
				});

			}
		}, {
			text : 'Confirm',
			id : 'confirmPurchConfirmBtn',
			itemId : 'confirmButton',
			disabled : true,
			handler : function() {
				var form = this.up('form').getForm();
				form.url = 'confPurchase.action';

				instTypeStore = Ext.data.StoreManager.lookup('confirmPurchInsTypeStore');

				var recor = instTypeStore.getAt(instTypeStore.find('id', Ext.getCmp('confirmPurchInstrumentType').getValue()));
				ConfPurchase.fieldDisableByInstrumentType(recor);
				console.log(recor);
				if (recor.get('withholdingTax') == 'No') {
					Ext.getCmp('confirmWithholdingTaxRate').disable();
					Ext.getCmp('confirmWithholdingTaxRate').setValue("");
					Ext.getCmp('confirmWithholdingTaxRate').allowBlank = true;
				} else {
					Ext.getCmp('confirmWithholdingTaxRate').enable();
					Ext.getCmp('confirmWithholdingTaxRate').setValue("");
					Ext.getCmp('confirmWithholdingTaxRate').allowBlank = false;
				}
				if (recor.get('couponAvailability') == 0) {
					Ext.getCmp('confirmPurchCouponRate').setDisabled(true);
					Ext.getCmp('confirmPurchCouponRate').allowBlank = true;
					Ext.getCmp('confirmPurchCouponFreq').hide();
					Ext.getCmp('confirmDealCoupnData').hide();
					Ext.getCmp('confirmDealCoupnData').disable();
					// Ext.getCmp('//confirmPurchCpnDts,recordDealDtStore').
				} else {
					Ext.getCmp('confirmPurchCouponRate').setDisabled(false);
					Ext.getCmp('confirmPurchCouponRate').allowBlank = false;
					Ext.getCmp('confirmPurchCouponFreq').show();
					Ext.getCmp('confirmDealCoupnData').show();
					Ext.getCmp('confirmDealCoupnData').enable();
				}
				if (recor.get('istrumentType') == 1) {
					Ext.getCmp('recordDealPrimDealer').setDisabled(false);
					Ext.getCmp('recordDealPrimDealer').allowBlank = false;
					Ext.getCmp('recordDealIssuer').setDisabled(true);
					Ext.getCmp('recordDealIssuer').allowBlank = true;
				} else {
					Ext.getCmp('recordDealPrimDealer').setDisabled(true);
					Ext.getCmp('recordDealPrimDealer').allowBlank = true;
					Ext.getCmp('recordDealIssuer').setDisabled(false);
					Ext.getCmp('recordDealIssuer').allowBlank = false;
				}

				if (recor.get('interestRate') == 'Fixed') {
					Ext.getCmp('confirmPurchFloatField1').hide();
					Ext.getCmp('confirmPurchFloatField2').hide();
					Ext.getCmp('confirmPurchFloatField1').disable();
					Ext.getCmp('confirmPurchFloatField2').disable();
					Ext.getCmp('confirmPurchResetPeriod').allowBlank = true;
					Ext.getCmp('confirmPurchFloatResetDt').allowBlank = true;
					Ext.getCmp('confirmPurchFloor').allowBlank = true;
					Ext.getCmp('confirmPurchCeiling').allowBlank = true;
					Ext.getCmp('confirmPurchResetPeriod').disable();
					Ext.getCmp('confirmPurchFloatResetDt').disable();
					Ext.getCmp('confirmPurchFloor').disable();
					Ext.getCmp('confirmPurchCeiling').disable();
				} else {
					Ext.getCmp('confirmPurchFloatField1').show();
					Ext.getCmp('confirmPurchFloatField2').show();
					Ext.getCmp('confirmPurchFloatField1').enable();
					Ext.getCmp('confirmPurchFloatField2').enable();
					Ext.getCmp('confirmPurchResetPeriod').allowBlank = false;
					Ext.getCmp('confirmPurchFloatResetDt').allowBlank = false;
					Ext.getCmp('confirmPurchFloor').allowBlank = false;
					Ext.getCmp('confirmPurchCeiling').allowBlank = false;
					Ext.getCmp('confirmPurchResetPeriod').enable();
					Ext.getCmp('confirmPurchFloatResetDt').enable();
					Ext.getCmp('confirmPurchFloor').enable();
					Ext.getCmp('confirmPurchCeiling').enable();
				}

				if (recor.get('listed') == 0) {
					Ext.getCmp('confirmPurchInstrumentCodeCombo').disable();
					Ext.getCmp('confirmPurchInstrumentCodeText').enable();
				} else {

					Ext.getCmp('confirmPurchInstrumentCodeText').disable();
					Ext.getCmp('confirmPurchInstrumentCodeCombo').enable();
				}

				Ext.getCmp('confPurchaseSettlementMethod').enable();
				Ext.getCmp('confPurchaseDealer').enable();
				Ext.getCmp('confPurchaseDelevaryMethod').enable();
				Ext.ComponentQuery.query('#confirmPurchFund')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchInsName')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchValueDt')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchInstrumentType')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchDealVal')[0].enable();
				Ext.getCmp('confirmPurchMaturityValue').enable();
				Ext.ComponentQuery.query('#confirmPurchPlacementFee')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchInterestRate')[0].enable();
				Ext.ComponentQuery.query('#confirmPurchDayBasis')[0].enable();

				if (form.isValid()) {
					formPanel.setLoading('Confirming...');
					var couponDates = '';
					Ext.data.StoreManager.lookup('couponDatesStore').each(function(r) {
						couponDates += Ext.util.Format.date(r.get('couponDate')) + ';';
					});
					var rstDt = Ext.ComponentQuery.query('#confirmPurchFloatResetDt')[0];
					var valueDtField = Ext.ComponentQuery.query('#confirmPurchValueDt')[0];
					var maturityDtField = Ext.ComponentQuery.query('#confirmPurchMaturityDt')[0];
					var dealCode = Ext.ComponentQuery.query('#confirmPurchDealID')[0].text.substr(9);
					var params = (Ext.ComponentQuery.query('#confirmPurchParamLbl')[0]).text;
				
					form.submit({
						params : {
							'couponDates' : couponDates,
							'resetDate' : Ext.util.Format.date(rstDt.getValue()),
							'valueDate' : Ext.util.Format.date(valueDtField.getValue()),
							'maturityDate' : Ext.util.Format.date(maturityDtField.getValue()),
							'dealCode' : dealCode,
							'user' : 'confirmedUser',
							'params' : params,
						},
						success : function(form, action) {
							form.reset();
							Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
							Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
							Ext.Msg.alert('Success', action.result.name + ' is confirmed.<br> Deal Code is <b>' + action.result.dealCode + '</b>');
							formPanel.setLoading(false);
							Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
							Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
							Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
							formPanel.getForm().getFields().each(function(field) {
								field.setDisabled(true);
							});
							Ext.getCmp('confirmPurchConfirmBtn').disable();
							Ext.getCmp('confirmPurchRejectBtn').disable();
							Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						},
						failure : function(form, action) {
							form.reset();
							Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
							Ext.Msg.alert('Failed', 'There is some error returned from the server.<br><b>' + action.result.status + '</b>');
							formPanel.setLoading(false);
							Ext.ComponentQuery.query('#confirmPurchDuration')[0].setText('');
							Ext.ComponentQuery.query('#confirmPurchDealID')[0].setText('');
							Ext.ComponentQuery.query('#confirmPurchListedLabel')[0].setText('');
							formPanel.getForm().getFields().each(function(field) {
								field.setDisabled(true);
							});
							Ext.getCmp('confirmPurchConfirmBtn').disable();
							Ext.getCmp('confirmPurchRejectBtn').disable();
							Ext.ComponentQuery.query('#confirmPurchTrigField')[0].enable();
						}
					});
				} else {
					Ext.Msg.alert("ERROR!", "Form Incomplete. Please check!");
				}
			}
		} ]
	});
	return formPanel;
};

ConfPurchase.getRollOverDeal = function(instumentType, fund, dealCode, issuer, valueDate, principleValue) {
	var panel = ConfPurchase.getCenterPanel();
	var tmpInsTypeStore = Ext.data.StoreManager.lookup('confirmPurchInsTypeStore');
	var tmpFundStore = Ext.data.StoreManager.lookup('dealFundsStore');
	var tmpPrimeDealerStore = Ext.data.StoreManager.lookup('primaryDealersStore');
	var tmpIssuerStore = Ext.data.StoreManager.lookup('issuersStore');

	var tmpPrimDealerCmb = Ext.getCmp('recordDealPrimDealer');
	var tmpIssuerCmb = Ext.getCmp('recordDealIssuer');
	var insTypeCmb = Ext.getCmp('confirmPurchInstrumentType');
	var fundCmb = Ext.getCmp('confirmPurchFund');
	var tmpValDt = Ext.getCmp('confirmPurchValueDt');
	var tmpDealValue = Ext.getCmp('confirmPurchDealVal');
	var paramLabel = Ext.getCmp('confirmPurchParamLbl');

	Ext.Ajax.request({
		url : 'getInstrumentTypes.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			tmpInsTypeStore.loadRawData(obj.instrumentTypeList);
			insTypeCmb.setValue(tmpInsTypeStore.getAt(tmpInsTypeStore.find('instrumentName', instumentType)).get('id'));
			var insTypeRecord = tmpInsTypeStore.findRecord('instrumentName', instumentType);
			insTypeCmb.fireEvent('select', insTypeCmb, insTypeRecord);
			Ext.Ajax.request({
				url : 'getFundsForDealInsType.action',
				params : {
					querry : 'PURCH',
					querryTwo : insTypeCmb.getValue(),
				},
				success : function(response) {
					var obj = Ext.decode(response.responseText);
					Ext.data.StoreManager.lookup('dealFundsStore').loadRawData(obj.fundList);
					fundCmb.setValue(tmpFundStore.getAt(tmpFundStore.find('fundCode', fund)).get('id'));
					if (!tmpPrimDealerCmb.isDisabled()) {
						tmpPrimDealerCmb.setValue(tmpPrimeDealerStore.getAt(tmpPrimeDealerStore.find('code', issuer)).get('id'));
					} else {
						tmpIssuerCmb.setValue(tmpIssuerStore.getAt(tmpIssuerStore.find('code', issuer)).get('id'));
					}
				},
				failure : function(response) {
					Ext.Msg.alert('Error!', 'Failed to Load Fund!');
				}
			});
		},
		failure : function(response) {
			Ext.Msg.alert('Error!', 'Failed to Load Instrument Type!');
		}
	});

	tmpValDt.setValue(valueDate);
	tmpDealValue.setValue(principleValue);
	paramLabel.setText(dealCode);

	return panel;
};

ConfPurchase.fieldDisableByInstrumentType = function(record) {

	if (record != null || record != "") {
		var form = Ext.getCmp('confinstrumentdetails');
		if (record.get('dayBasis') == 'Vary') {
			Ext.getCmp('confirmPurchDayBasis').enable();
		} else {
			Ext.getCmp('confirmPurchDayBasis').disable();
		}

		if (record.get('interestRate') == "Fixed") {
			Ext.getCmp('confirmPurchFloatField1').hide();
			Ext.getCmp('confirmPurchFloatField2').hide();
			Ext.getCmp('confirmPurchFloatField1').disable();
			Ext.getCmp('confirmPurchFloatField2').disable();
			Ext.getCmp('confirmPurchResetPeriod').allowBlank = true;
			Ext.getCmp('confirmPurchFloatResetDt').allowBlank = true;
			Ext.getCmp('confirmPurchFloor').allowBlank = true;
			Ext.getCmp('confirmPurchCeiling').allowBlank = true;
			Ext.getCmp('confirmPurchResetPeriod').disable();
			Ext.getCmp('confirmPurchFloatResetDt').disable();
			Ext.getCmp('confirmPurchFloor').disable();
			Ext.getCmp('confirmPurchCeiling').disable();
		} else {
			Ext.getCmp('confirmPurchFloatField1').show();
			Ext.getCmp('confirmPurchFloatField2').show();
			Ext.getCmp('confirmPurchFloatField1').enable();
			Ext.getCmp('confirmPurchFloatField2').enable();
			Ext.getCmp('confirmPurchResetPeriod').allowBlank = false;
			Ext.getCmp('confirmPurchFloatResetDt').allowBlank = false;
			Ext.getCmp('confirmPurchFloor').allowBlank = false;
			Ext.getCmp('confirmPurchCeiling').allowBlank = false;
			Ext.getCmp('confirmPurchResetPeriod').enable();
			Ext.getCmp('confirmPurchFloatResetDt').enable();
			Ext.getCmp('confirmPurchFloor').enable();
			Ext.getCmp('confirmPurchCeiling').enable();
		}
		form.getForm().loadRecord(record);

		Ext.getCmp('confirmPurchCouponFreq').setValue(record.get('cuponFrequency'));

		var lblListed = Ext.getCmp('confirmPurchListedLabel');
		var comboInsCode = Ext.getCmp('confirmPurchInstrumentCodeCombo');
		var tfInsCode = Ext.getCmp('confirmPurchInstrumentCodeText');

		if (record.get('listed') == 1) {
			lblListed.setValue('Listed Instrument');
			tfInsCode.disable();
			tfInsCode.hide();
			comboInsCode.show();
			comboInsCode.enable();
			Ext.Ajax.request({
				url : 'getInstrumentsByType.action',
				params : {
					"instrumentTypeId" : record.get('id')
				},
				success : function(response) {
					var obj = Ext.decode(response.responseText);
					confinstrumentStore.removeAll();
					confinstrumentStore.loadRawData(obj.instrumentDtos);
				}
			});

		} else {
			lblListed.setValue('Non-Listed Instrument');
			comboInsCode.hide();
			comboInsCode.disable();
			tfInsCode.show();
			tfInsCode.enable();
		}

		var maturityValuetf = Ext.getCmp('confirmPurchMaturityValue');
		var dealValuetf = Ext.getCmp('confirmPurchDealVal');
		if (record.get('calculationInput') == 2) {
			dealValuetf.setReadOnly(true);
			maturityValuetf.setReadOnly(false);
			confrimcalculationtype = "MATURITY";
		} else {
			maturityValuetf.setReadOnly(true);
			dealValuetf.setReadOnly(false);
			confrimcalculationtype = "DEAL";
		}
	}

};
ConfPurchase.setInstrumentValue = function(record) {

	var dfValDate = Ext.getCmp('confirmPurchValueDt');
	dfValDate.format = 'd/m/Y';
	dfValDate.setValue(record.get('valueDate'));

	var dfMatDate = Ext.getCmp('confirmPurchMaturityDt');
	dfMatDate.format = 'd/m/Y';
	dfMatDate.setValue(record.get('maturityDate'));

	Ext.getCmp('confirmPurchCouponRate').setValue(record.get('couponRate'));
	Ext.getCmp('confirmPurchDenominamMinVal').setValue(record.get('minDenominal'));

};